package com.ontheroad.mysql.user.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.modoumama.service.base.impl.BaseServiceImpl;

import com.ontheroad.api.response.Response;
import com.ontheroad.mysql.central.mapper.CentralBindTopicMapper;
import com.ontheroad.mysql.user.mapper.UserTopicMapper;
import com.ontheroad.user.model.UserDto;
import com.ontheroad.user.model.UserEntity;
import com.ontheroad.user.service.UserTopicService;
import com.ontheroad.utils.RegexUtil;
import com.ontheroad.utils.UUIDUtil;

/*import com.ontheroad.utils.SmsUtil;*/

@Service
@Transactional
public class UserTopicServiceImpl extends BaseServiceImpl<UserEntity, String>
		implements UserTopicService {

	private Logger log = LoggerFactory.getLogger(getClass());

	private final String TEMP_CODE = "123456";

	@Autowired
	private UserTopicMapper userTopicMapper;
	
	@Autowired
	private CentralBindTopicMapper centralBindTopicMapper;

	public HashMap<String, Object> regist(UserDto userDto) throws Exception {
		HashMap<String, Object> bean  =new HashMap<String, Object>();
		log.info("用户注册");
		if (userDto.getPassword()==null | userDto.getUser()==null) {
			bean.put("code", 1);
			bean.put("message", "参数不能为空");
			return bean;
		}
		if (!RegexUtil.checkPhone(userDto.getUser())) {
			bean.put("code", 1);
			bean.put("message", "手机号格式不正确");
			return bean;
		}
		
		String mobile = userDto.getUser();
		// 验证码
		String verificache;
		// TODO: 临时测试用，上线后要删除该代码
		verificache = TEMP_CODE;
		String verification = userDto.getSmscode();
		if (!verificache.equals(verification)) {
			bean.put("code", 1);
			bean.put("message", "验证码错误");
			return bean;
		}
		// 手机号是否已被注册
		UserEntity userEntity = userTopicMapper.getUserByMobile(mobile);
		if (userEntity != null) {
			bean.put("code", 1);
			bean.put("message", "手机号已被注册");
			return bean;
		}
		// 密码加密存储
//		String md5Pwd = Md5Util.makeMd5Sum(userDto.getPassword().getBytes());

		// 插入用户基本信息
		UserEntity user = new UserEntity();
		user.setId(UUIDUtil.generate());
		user.setPassword(userDto.getPassword());
		user.setUser(mobile);
		user.setIsEnable(true);
		user.setUpdateTime(new Date());
		user.setCreateTime(new Date());
		user.setLevel(1);
		userTopicMapper.insertModel(user);
		bean.put("code", 0);
		bean.put("message", "success");
		return bean;
	}

	public HashMap<String, Object>  login(UserDto userDto) throws Exception {
		HashMap<String, Object> bean  =new HashMap<String, Object>();
		if (userDto.getPassword()==null | userDto.getUser()==null) {
			bean.put("code", 1);
			bean.put("message", "参数不能为空");
			return bean;
		}
		if (!RegexUtil.checkPhone(userDto.getUser())) {
			bean.put("code", 1);
			bean.put("message", "手机号格式不正确");
			return bean;
		}
		
		UserEntity userEntity = userTopicMapper.getUserByMobile(userDto
				.getUser());
		if (userEntity == null) {
			bean.put("code", 1);
			bean.put("message", "手机号未注册");
			return bean;
		}
		if (!userEntity.getPassword().equals(userDto.getPassword())) {

			bean.put("code", 1);
			bean.put("message", "密码错误");
			return bean;
		}
		List<String> centrals = centralBindTopicMapper
				.findMacByUser(userDto.getUser());
		bean.put("code", 0);
		bean.put("message", "success");
		bean.put("centrals", centrals);
		return bean;
	}

	public Response ThirdLogin(UserDto userDto) throws Exception {
		
		return null;
	}

	public HashMap<String, Object> resetPasswrod(UserDto userDto)
			throws Exception {
		HashMap<String, Object> bean  =new HashMap<String, Object>();
		if (userDto.getPassword()==null | userDto.getUser()==null) {
			bean.put("code", 1);
			bean.put("message", "参数不能为空");
			return bean;
		}
		if (!RegexUtil.checkPhone(userDto.getUser())) {
			bean.put("code", 1);
			bean.put("message", "手机号格式不正确");
			return bean;
		}
		String mobile = userDto.getUser();
		// 验证码
		String verificache;
		// TODO: 临时测试用，上线后要删除该代码
		verificache = TEMP_CODE;
		String verification = userDto.getSmscode();
		if (!verification.equals(verificache)) {
			bean.put("code", 1);
			bean.put("message", "验证码错误");
			return bean;
		}
		UserEntity userEntity = userTopicMapper.getUserByMobile(mobile);
		if (userEntity == null) {
			bean.put("code", 1);
			bean.put("message", "手机号未注册");
			return bean;
		}
		UserEntity user = new UserEntity();
		user.setUser(userDto.getUser());
		user.setPassword(userDto.getPassword());
		userTopicMapper.modifyUser(user);
		bean.put("code", 0);
		bean.put("message", "success");
		return bean;
	}

}
